<?php
//---------------------------------------------------------------------------------------------------
    require_once('../../config.inc.php');

    require_once($config['app_dir'] . '/includes/date.class.php');
    require_once($config['app_dir'] . '/includes/timer.class.php');
    require_once($config['app_dir'] . '/classes/sparklines.deamon.php');
    require_once($config['app_dir'] . '/classes/forecasts.deamon.php');
    require_once($config['app_dir'] . '/classes/stockplot.data.php');
    require_once($config['app_dir'] . '/classes/StockMailers.class.php');
    //-----------------------------------------------------------------------------------------------
    $show = true;
    $run_service = true;
    $run_mailer = true;
    $run_rebuild = false;
    $run_send = true;
    $run_forecast = true;
    $run_sparklinein = true;
    $run_sparklineout = false;
    //-----------------------------------------------------------------------------------------------
    set_time_limit(0);
    $Timer = new objTimer(true);
    $user = false;
    $password = false;

    if( 'cli'==php_sapi_name() ) { //console access
        $trade = intval($argv[1]);
        $start = intval($argv[2]);
        $finish = intval($argv[3]);
        $system = intval($argv[4]);
        $forecast_id =  intval($argv[5]);
        $delim = "\n";
    } else { //web access
        header('Content-Type: text/html; charset=utf-8');
        $user = strval($_GET['user']);
        $password = strval($_GET['password']);
        if( $config['manual']['user']!=$user || md5($config['manual']['password'])!=md5($password) ) die('user not set');
        $StockParams = new objUStockParams(true, false);

        $trade = $StockParams->trade;
        $start = $StockParams->start;
        $finish = $StockParams->finish;
        $system= $StockParams->system;
        $forecast_id = $StockParams->forecast_id;
        $delim = "<br>";
    }//
    //-----------------------------------------------------------------------------------------------
    $sstart = $start;

    $ufinish = false;
    if( $forecast_id ) {
        $Forecast = new objForecastView();
        $Forecast->id = $forecast_id;

        $forecastFound = $Forecast->Load();
        // if forecast not found stop script
        if (empty( $forecastFound)) exit ("Forecast not found in DB!<br>");

        $ufinish = $Forecast->EndTime;
        if( !$system ) $system = $Forecast->SystemID;
        if( !$trade ) $trade = $Forecast->TradeID;
        if( !$sstart ) {
           $sstart = $Forecast->BeginTime;
        }
    } //if set forecast id

    if( $finish && !$ufinish ) {
        $ufinish = $finish;
    }

    if( $ufinish ) {
        $ChartPeriod = new objChartPeriod($system);
        $ChartPeriod->setMaxDate($ufinish);
        $period = $ChartPeriod->GetPeriod();

        $start = $period['start'];
        $finish = $period['finish'];
    }//if end time found
    var_dump($period);
    if( !$finish ) die('Period set not correctly');

    //-----------------------------------------------------------------------------------------------
    $Timer->finish();

    if( $show ) {
        echo "<hr>Input parameters: <pre>";
        echo 'Trade';
        var_dump($trade);
        echo 'Start';
        var_dump($start);
        echo 'Finish';
        var_dump($finish);
        echo 'System';
        var_dump($system);
        echo "\n (".$Timer->time()." s)\n";
        echo "</pre><hr>";
    } //if show
    //-----------------------------------------------------------------------------------------------
    if($run_service ) {
        $Timer->start();
        if( $show ) echo "Service mailer: --start-- " . $delim;
        try {
            $Mailer = new objExportServiceProcess($trade, $sstart, 1, $system);
            $Mailer->Send();
        } catch(exception $e) {
            die('Error: '.$e->getMessage());
        }
        $Timer->finish();
        if( $show ) echo $delim . "Service mailer: (".$Timer->time()." s) --done--" . $delim;
    } //service
    //-----------------------------------------------------------------------------------------------
    try {
        if( $run_mailer ) {
            $Timer->start();
            if( $show ) echo "Param mailer: --start-- " . $delim;
            $StockMailer = new objStockParamMailer($trade,
                DateConvert::Date('Y-m-d', $start),
                DateConvert::Date('Y-m-d', $finish), -1, $system);
            $StockMailer->show = $show;
            if( $forecast_id) $StockMailer->forecast_id = $forecast_id;
            $StockMailer->Send();
            $mSessionID = $StockMailer->mail_session_id;
            $Timer->finish();
            if( $show ) echo $delim . "Param mailer: (".$Timer->time()." s) --done--" . $delim;
        } //mailer
        //-----------------------------------------------------------------------------------------------
        if( $run_rebuild ) {
            $Timer->start();
            if( $show ) echo "Param mailer (rebuild): --start-- " . $delim;
            $StockMailer = new objStockParamMailer($trade,
                DateConvert::Date('Y-m-d', $start),
                DateConvert::Date('Y-m-d', $finish), -1, $system);
            $StockMailer->show = $show;
            if( $forecast_id) $StockMailer->forecast_id = $forecast_id;
            $StockMailer->mail_session_id = $mSessionID;
            $StockMailer->Send();
            $Timer->finish();
            if( $show ) echo $delim . "Param mailer (rebuild): (".$Timer->time()." s) --done--" . $delim;
        } //rebuild
        //-----------------------------------------------------------------------------------------------
        if( $run_send ) {
            $Timer->start();
            if( $show ) echo "Resend mailer: --start-- " . $delim;
            $ReMailer = new objResendMailer($mSessionID);
            $ReMailer->max_tries = 4;
            $ReMailer->sleep_time = 10;
            $ReMailer->loging = true;
            $ReMailer->log_prefix = 'param_';
            $ReMailer->show = $show;
            $ReMailer->Send();
            $Timer->finish();
            if( $show ) echo $delim . "Resend mailer: (".$Timer->time()." s) --done--" . $delim;
        } //send
    } catch(exception $e) {
        echo $e->getMessage();
        die('Error: '.$e->getMessage());
    }
    //-----------------------------------------------------------------------------------------------
    if( $run_forecast ) {
        $Timer->start();
        if( $show ) echo "Last forecast deamon: --start-- " . $delim;
        $LastForecastDeamon = new objLastForecastDeamon();
        $LastForecastDeamon->Render();
        $Timer->finish();
        if( $show ) echo $delim . "Last forecast deamon: (".$Timer->time()." s) --done--". $delim;
    } //forecast
    //-----------------------------------------------------------------------------------------------
    if( $run_sparklinein ) {
        $Timer->start();
        if( $show ) echo "SparklineIn deamon:  --start-- " . $delim;
        $LastSparklineInDeamon = new objLastSparklineInDeamon();
        $LastSparklineInDeamon->Render();
        $Timer->finish();
        if( $show ) echo $delim . "SparklineIn deamon: (".$Timer->time()." s) --done--" . $delim;
    } //sparklinin
    //-----------------------------------------------------------------------------------------------
    if( $run_sparklineout ) {
        $Timer->start();
        if( $show ) echo "SparklineOut deamon: --start-- ";
        $LastSparklineOutDeamon = new objLastSparklineOutDeamon();
        $LastSparklineOutDeamon->Render();
        $Timer->finish();
        if( $show ) echo $delim . "SparklineOut deamon: (".$Timer->time()." s) --done--". $delim;
    } //sparklinout
    //-----------------------------------------------------------------------------------------------
    if( $show ) echo $delim . "--Finished--" . $delim;
//---------------------------------------------------------------------------------------------------
?>
